package com.wjh.graduation.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wjh.graduation.domain.Groups;
import com.wjh.graduation.domain.People;
import com.wjh.graduation.domain.R;
import com.wjh.graduation.service.GroupsService;
import com.wjh.graduation.service.PeopleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 分组 前端控制器
 * </p>
 *
 * @author Wu Jiahong
 * @since 2022-01-25
 */
@RestController
@RequestMapping("/group")
@Api(tags = "分组相关接口")
public class GroupsController {

	@Autowired
	GroupsService groupsService;

	@Autowired
	PeopleService peopleService;

	@GetMapping("/getGroup")
	@ApiOperation("获得用户分组")
	public R getGroup(@ApiParam("传入用户的id") @RequestParam Integer id) {
		QueryWrapper<Groups> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_id", id);
		List<Groups> list = groupsService.list(queryWrapper);
		if (list.isEmpty()) {
			return new R(303, null, "获取失败");
		} else {
			return new R(200, list, "获取成功");
		}
	}

	@ApiOperation("新建分组")
	@PostMapping("/create")
	public R creatGroup(@Validated @RequestBody @ApiParam(name = "传入分组的名称、用户ID") Groups group) {
		if (groupsService.save(group)) {
			return new R(200, "添加成功");
		} else {
			return new R(303, "添加失败");
		}
	}

	@ApiOperation("删除分组")
	@GetMapping("/delete")
	@Transactional
	public R deleteGroup(@ApiParam("分组的ID") @RequestParam Integer groupId, @ApiParam("用户的ID") @RequestParam Integer userId) {
		//如果删除了，要把与分组有关的人脉放到默认分组
		//查询要修改的分组目的id
		QueryWrapper<Groups> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_id", userId).eq("name", "默认分组");
		Groups groups = groupsService.getOne(queryWrapper);
		if (groups != null) {
			//找到默认分组的id，构建要更新的条件
			UpdateWrapper<People> updateWrapper = new UpdateWrapper<>();
			updateWrapper.eq("group_id", groupId).set("group_id", groups.getGroupId());
			//先判断分组是否有人脉，没有直接删除
			QueryWrapper<People> qw = new QueryWrapper<>();
			qw.eq("user_id", userId).eq("group_id", groupId);
			if (peopleService.count(qw) == 0) {
				//无人脉在这个分组，直接删除
				if (groupsService.removeById(groupId)) {
					return new R(200, "删除成功");
				} else {
					//无人脉在这个分组但删除失败，让事务回滚
					throw new NullPointerException();
				}
			} else {
				//更新操作,注意更新的是人脉
				if (peopleService.update(updateWrapper)) {
					//更新成功，开始删除
					if (groupsService.removeById(groupId)) {
						return new R(200, "删除成功");
					} else {
						//更新成功但删除失败，让事务回滚
						throw new NullPointerException();
					}
				} else {
					//更新人脉为默认分组失败
					return new R(303, "删除失败");
				}
			}
		} else {
			//查询默认分组出错
			throw new NullPointerException();
		}
	}

	@ApiOperation("修改分组")
	@PostMapping("/update")
	public R updateGroup(@Validated @ApiParam(name = "传入分组的名称、ID、用户ID") @RequestBody Groups group) {
		if (groupsService.updateById(group)) {
			return new R(200, "修改成功");
		} else {
			return new R(303, "修改失败");
		}
	}


}

